
import abc
import unittest

from pluginlib.context import Context
from pluginlib.plugin import Interface, Implementation

class  Context_De_ActivateTest(unittest.TestCase):
    def setUp(self):
        class TestInterface(Interface):
            def a_method(self):
                pass

        class TestImplementation(Implementation):
            iface_name = 'TestInterface'
            def a_method(self):
                pass

        class TestInterface2(Interface):
            def test(self):
                pass

        self.iface = TestInterface
        self.impl = TestImplementation
        self.iface2 = TestInterface2

        self.ctx = Context()
        self.plug1_ctx = self.ctx.new_plugin_context()
        self.plug2_ctx = self.ctx.new_plugin_context()

    def test_unrelated(self):
        self.plug1_ctx.add_interface(self.iface)
        self.plug2_ctx.add_interface(self.iface2)

        self.assertEqual(None, self.ctx.get_interface('TestInterface'))
        self.assertEqual(None, self.ctx.get_interface('TestInterface2'))

        self.plug1_ctx.activate()
        self.plug2_ctx.activate()

        self.assertEqual(self.iface, self.ctx.get_interface('TestInterface'))
        self.assertEqual(self.iface2, self.ctx.get_interface('TestInterface2'))

    def test_iface_impl(self):
        self.plug1_ctx.add_interface(self.iface)
        self.plug2_ctx.add_implementation(self.impl)

        self.assertFalse(self.impl in
                                 self.ctx.get_implementations('TestInterface'))
        self.assertFalse(issubclass(self.impl, self.iface))

        self.plug1_ctx.activate()
        self.plug2_ctx.activate()

        iface = self.ctx.get_interface('TestInterface')
        impl = self.ctx.get_implementation('TestInterface',
                                           'TestImplementation')

        self.assertEqual(self.iface, iface)
        self.assertEqual(self.impl, impl)

        self.assertTrue(self.impl in
                                 self.ctx.get_implementations('TestInterface'))
        self.assertTrue(issubclass(self.impl, self.iface))
        self.assertTrue(issubclass(impl, iface))

if __name__ == '__main__':
    unittest.main()

